// const base = 'https://drive.hn8090kj.com/';
const base = 'https://e.epeijia.com/';
const ajaxbase = base;
export const publicFunction = {
	hrefURL: ajaxbase,
	URLimg: base,
	appid:'wx1d6c7f6df0f6884c',
	appsecret:'cae67bec0402344e29c30121dd27a5a8',
	serveimg:'../../static/serve/',  // 静态图
	uploadimage: `${ajaxbase}/coach/file/upload`, // 上传图片
	// 登录注册
	login_send_code: `/coach/sms/sendSms`, // 发送验证码

	testphone: new RegExp(/^1(3|4|5|6|7|8|9)[0-9]{9}$/), //手机号
	testcard: new RegExp(/^(\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/), //身份证
	testnum: new RegExp(/^[0-9]+.?[0-9]*/), //数字
	limit: 20,
	//获取当前时间戳（到秒）
	getTime10: () => {
		var timestamp = Date.parse(new Date());
		return timestamp / 1000;
	},

	toast: (titles) => {
		if (!typeof plus) {
			plus.nativeUI.toast(titles, {
				verticalAlign: 'center'
			})
		} else {
			setTimeout(() => {
				uni.showToast({
					icon: 'none', //success、loading、none
					title: titles,
					duration: 2000
				})
			}, 100)
		}
	},
	showModal: (title, content, callback) => {
		let titles = title || '提示';
		let contents = content || '模态弹窗';
		uni.showModal({
			title: titles,
			content: contents,
			success: (res) => {
				if (res.confirm) {
					callback(res)
				}
			}
		});
	},
	/*
	animationType :
		zoom-fade-out: 新窗体从小到大逐渐放大并且从透明到不透明逐渐显示
		pop-in:新窗体从左侧进入，且老窗体被挤压而出
		fade-in:新窗体从透明到不透明逐渐显示
	*/
	navigate: (path, params) => {
		let paramsData = params || '';
		let urls = path + '?' + paramsData;
		uni.navigateTo({
			url: urls,
			animationType: 'zoom-fade-out', // 窗口动画,只在APP有效 pop-in、fade-in
			animationDuration: 300, // 窗口动画持续时间,单位毫秒,只在APP有效
		})
	},
	goback: (num) => {
		uni.navigateBack({
			delta: num || 1
		});
	},
	maxphotofun:(photourl,type=1)=>{
		if(!photourl){
			uni.showModal({
				title: '提示',
				content: '路径错误',
				showCancel: false
			})
			return false
		}
		let arr = [];
		if(type == 1){
			arr.push(photourl)
		}else{
			arr = photourl
		};
		uni.previewImage({
			urls:arr
		})
	},
	uploadFileimg: (callback, that) => {
		let thats = this;
		uni.chooseImage({
			count: 1,
			sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
			sourceType: ['album'], //从相册选择  camera 使用相机
			success: (res) => {
				let tempFilePaths = res.tempFilePaths[0];
				uni.showLoading({
					mask: true,
					title: '上传中...'
				});
				uni.uploadFile({
					url: publicFunction.uploadimage,
					filePath: tempFilePaths,
					name: 'file',
					header: {
						"Authorization": uni.getStorageSync('token') || ''
					},
					success(res) {
						uni.hideLoading();
						let datas = JSON.parse(res.data);
						callback(datas);
					}
				});
			},
			fail: (err) => {
				console.log('图片上传-', err)
			}
		});
		setTimeout(() => {
			uni.hideLoading();
		}, 20000)
	},
	/*
	url, met, datas, sfun：必传项
	showloading:是否加载加载窗 （Boolean 选填  false）
	istoken：优先检查token，当无需token也可加载数据时将参数改为true （Boolean 选填  false）
	*/
	unirequest: (url, met, datas, sfun, showloading=false, istoken=false) => {
		if (!istoken) {
			if (!uni.getStorageSync('token')) {
				publicFunction.navigate('/pages/start/login');
				return false
			}
		};
		var headers = {
			'content-type': 'application/json',
			"Authorization": uni.getStorageSync('token'),
			// "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
			// 'token-type': 0, //0正常token验证1跳过验证（公众号可使用）,
		}
		// if (met.toUpperCase() == 'POST') {}
		if (!showloading) {
			uni.showLoading({
				mask: true,
				title: '请稍后...'
			});
		};
		// datas._time = Date.parse(new Date()) / 1000;
		// datas.phone_type = '3';
		// datas.device = 'mi';
		// datas._sign = publicFunction.encrypt(datas);
		uni.request({
			url: ajaxbase + url,
			method: met || 'POST',
			data: datas,
			header: headers,
			dataType: 'json',
			responseType: 'text',
			success: (res) => {
				let datas = res.data;
				if (datas.code === -1) {
					publicFunction.toast('您尚未登录；前去登录！');
					setTimeout(() => {
						publicFunction.navigate('/pages/start/login');
					}, 2000)
				} else {
					sfun(datas)
				}
				uni.hideLoading();
			},
			fail: (err) => {
				uni.hideLoading();
				console.log(err)
				// uni.showModal({
				// 	title: '提示',
				// 	content: '系统错误',
				// 	showCancel: false
				// })
			},
			complete: () => {
				uni.hideLoading();
			}
		});
		setTimeout(() => {
			uni.hideLoading();
		}, 59000)
	},

	/**
	 * desc    加密数据
	 * @property {Object}  obj	需要加密的对象
	 **/
	encrypt: (obj) => {
		let str = "";
		let dev;

		for (let k in this.sort_ASCII(obj)) {
			str += k + this.sort_ASCII(obj)[k]
		}
		let phone_type = 'android';
		if (obj.phone_type == 1) {
			phone_type = 'android'
		} else if (obj.phone_type == 2) {
			phone_type = 'ios'
		} else if (obj.phone_type == 3) {
			phone_type = 'web'
		}
		let signature = `aovnai${str}${phone_type}MDAwMD1AkbvwMDAwtMIJ7hZ`;
		return md5.hex_md5(md5.hex_md5(signature))
	},
	/**
	 * desc ascll码排序
	 * @property {Object}	需要排序的对象
	 **/
	sort_ASCII: (obj) => {
		var arr = new Array();
		var num = 0;
		for (var i in obj) {
			arr[num] = i;
			num++;
		}
		var sortArr = arr.sort();
		var sortObj = {};
		for (var i in sortArr) {
			sortObj[sortArr[i]] = obj[sortArr[i]];
		}
		return sortObj;
	}
}
const install = (Vue,options={}) => {
	// #ifdef VUE3
	Vue.config.globalProperties.apifun = publicFunction;
	// #endif
}
export default {
	publicFunction,
	install
};